package com.hankol.community

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseActivity
import com.baselibrary.bean.DynamicInfoData
import com.baselibrary.bean.SelectSendFriend4Bean
import com.baselibrary.bean.ShareItemBean
import com.baselibrary.extension.addTextChangedListenerClosure
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.toast
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.hankol.message.HKIMUtils
import com.hankol.community.databinding.ActivityShareFriendBinding


/**
 * @description 分享动态到好友
 * @author 关忆北、
 * @time 2022/3/31 3:17 下午
 */
@Route(path = ActivityRouter.COMMUNITY.COMMUNITY_SHARE_FRIEND)
class ShareFriendActivity : BaseActivity() {
    private var friendBean: ShareItemBean? = null
    private var dynamicBean: SelectSendFriend4Bean? = null
    private lateinit var mDataBinding: ActivityShareFriendBinding
    override fun getLayoutId(): Int {
        return R.layout.activity_share_friend
    }

    override fun init() {
        val attributes = window.attributes
        attributes.width = ScreenUtils.getScreenWidth() - ScreenUtils.dip2px(applicationContext, 29f) * 2
        window.attributes = attributes;
        mDataBinding = getDataBinding<ActivityShareFriendBinding>()
        if (intent.hasExtra("friend")) {
            friendBean = intent.getSerializableExtra("friend") as ShareItemBean
        }
        if (intent.hasExtra("dynamic")) {
            dynamicBean = intent.getParcelableExtra<SelectSendFriend4Bean>("dynamic")
        }
        intent.extras?.let {
            if (ARouterUtils.parseParams(it, "friend")[0].checkNotNull()) {
                val jsonBean: String = ARouterUtils.parseParams(it, "friend")[0]
                friendBean = GsonUtils.parseJsonToBean(jsonBean, ShareItemBean::class.java)
            }
            if (ARouterUtils.parseParams(it, "dynamic")[0].checkNotNull()) {
                val jsonBean: String = ARouterUtils.parseParams(it, "dynamic")[0]
                dynamicBean = GsonUtils.parseJsonToBean(jsonBean, SelectSendFriend4Bean::class.java)
            }

        }
        dynamicBean?.let {
            GlideUtils.loadImg_101(applicationContext, it.icon as String, mDataBinding.circleSharePrivateIcon)
            mDataBinding.tvSharePrivateName.text = it.title
            mDataBinding.tvShareAuthorName.text = it.name
            mDataBinding.tvSharePrivateDes.text = it.content
            GlideUtils.loadImg_101(applicationContext, it.cover, mDataBinding.roundSharePrivateCover)
            GlideUtils.loadImg_101(applicationContext, it.icon, mDataBinding.circleShareAuthorIcon)
            it.title = if (TextUtils.isEmpty(it.title)) it.content else it.title
        }
        mDataBinding.edtSharePrivateContent.addTextChangedListenerClosure(afterTextChanged = {
            if (it.toString().length == Constants.LIMIT_SHARE_WORD_NUM) {
                "消息字数限制1000字以下".toast()
            }
        })
        mDataBinding.tvSharePrivateCancel.setOnClickListener { finish() }
        mDataBinding.tvSharePrivateOk.setOnClickListener {
            val list = mutableListOf<String>()
            list.add(friendBean?.userId.toString())
            dynamicBean?.apply {
                val dynamicInfoData = DynamicInfoData(id, showType, cover, title, icon, name, isFans, route, liveState, mDataBinding.edtSharePrivateContent.text.toString())
                HKIMUtils.sendMessage4DynamicCard(list, dynamicInfoData)
                EventBusUtils.sendNotify(MessageBus.msgId_share_success)
                "分享成功".toast()
                finish()
            }
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        val dm = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(dm)
        val view: View = window.decorView
        val lp = view.layoutParams as WindowManager.LayoutParams
        lp.gravity = Gravity.CENTER
        windowManager.updateViewLayout(view, lp)
        //下面两行代码的顺序不可以改变不然圆角背景就设置不上了
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        view.setBackgroundResource(R.drawable.shape_rc_ffffff_8) //圆角背景
    }


}